extends Button

export var sl=32
export var pp=Vector2(-32,16)
export var u=0.2
export var chn="vbc"
export var movable=false
var ls=false#关闭
var ch
var op
var e
var sea=0
var s1ep=Vector2(0,32)
var les=0.0
var vbb=VBoxContainer.new()

func init(child_name=chn):
	self.connect("focus_entered",self,"fen")
	self.connect("focus_exited",self,"fex")
	self.connect("mouse_entered",self,"fen")
	self.connect("mouse_exited",self,"fex")
	self.connect("pressed",self,"pressed")
	chn=child_name
	ch=get_node(chn)
	set_chm(false)
	op=rect_position.x
	e=op
	movable=true
	ubd()

func ubd():
	if(!get_parent().is_a_parent_of(vbb)):
		get_parent().call_deferred("add_child",vbb)
		vbb.rect_size=Vector2(0,0)
		vbb.mouse_filter=Control.MOUSE_FILTER_IGNORE
	get_parent().call_deferred("move_child",vbb,get_index()+1)

func set_chm(s=false):
	setrec(self,s)

func setrec(n,s):
	var c=n.get_children()
	for t in c:
		if(t.is_class("Control")):
			t.mouse_filter=Control.MOUSE_FILTER_PASS if s else Control.MOUSE_FILTER_IGNORE
		setrec(t,s)

func _ready():
	init()

func fen():
	if(movable):
		e=op+sl
		sp()

func sp():
	if(get_parent().is_class("Node2D")):
		sys.pointer.e=Vector2(e+get_parent().global_position.x,rect_global_position.y)+pp
	elif(get_parent().is_class("Control")):
		sys.pointer.e=Vector2(e+get_parent().rect_global_position.x,rect_global_position.y)+pp
	else:
		sys.pointer.e=Vector2(e,rect_global_position.y)+pp

func fex():
	if(movable):
		e=op
		sp()

func _process(delta):
	if(movable):
		if(get_parent().is_class("Node2D")):
			if(rect_global_position.x!=e+get_parent().global_position.x):
				rect_global_position.x=lerp(rect_global_position.x,e+get_parent().global_position.x,u)
		elif(get_parent().is_class("Control")):
			if(rect_global_position.x!=e+get_parent().rect_global_position.x):
				rect_global_position.x=lerp(rect_global_position.x,e+get_parent().rect_global_position.x,u)
		else:
			if(rect_global_position.x!=e):
				rect_global_position.x=lerp(rect_global_position.x,e,u)
		if($s0.rotation_degrees!=sea||$s1.rotation_degrees!=sea):
			$s0.rotation_degrees=lerp($s0.rotation_degrees,sea,u)
			$s1.rotation_degrees=lerp($s1.rotation_degrees,sea,u)
		if($s1.position!=s1ep):
			$s1.position=lerp($s1.position,s1ep,u)
		if(ch.rect_scale.y!=les):
			sys.ds(String(ch.rect_scale.y)+"\n"+String(les))
			ch.rect_scale.y=lerp(ch.rect_scale.y,les,u)
			get_parent().c()
			sys.shr.rotation_degrees+=8
		if(vbb.rect_min_size.y!=ch.rect_size.y*ch.rect_scale.y):
			vbb.rect_min_size.y=lerp(vbb.rect_min_size.y,ch.rect_size.y*ch.rect_scale.y,u)
			vbb.rect_size.y=vbb.rect_min_size.y

func pressed():
	ls=!ls
	if(ls):
		set_chm(true)
		les=1.0
		sea=90
		s1ep=Vector2(0,40)
		e+=8
	else:
		set_chm(false)
		les=0.0
		sea=0
		s1ep=Vector2(0,32)
		e-=8
